﻿using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet.Formats.Biff.OfficeArt
{
    internal class OfficeArtDggContainer : OfficeArtRecord
    {
        public byte[] GetBytesNoHeader()
        {
            MemoryStream stream = new MemoryStream();
            using (BinaryWriter writer = new BinaryWriter(stream))
            {

                this.Write(new OfficeArtWriteContext(writer, null));
                writer.Flush();
                byte[] result = new byte[stream.Position];
                stream.Position = 0;
                stream.Read(result, 0, result.Length);
                return result;
            }
        }

        private OfficeArtBStoreContainer _blipStore;
        private OfficeArtColorMRUContainer _colorMru;
        private OfficeArtFDGGBlock _drawingGroup;
        private OfficeArtFOPT _drawingPrimaryOptions;
        private OfficeArtTertiaryFOPT _drawingTertiaryOptions;
        private OfficeArtSplitMenuColorContainer _splitColors = OfficeArtSplitMenuColorContainer.Default;

        public override RecType RecordType
        {
            get { return RecType.OfficeArtDggContainer; }
        }

        protected override byte Version
        {
            get { return 0x0F; }
        }

        protected override int ByteLength
        {
            get
            {
                int result = DrawingGroup.FullByteLength;
                if (BlipStore != null) result += BlipStore.FullByteLength;
                if (DrawingPrimaryOptions != null)
                result += DrawingPrimaryOptions.FullByteLength;
                if (_drawingTertiaryOptions != null) result += _drawingTertiaryOptions.FullByteLength;
                if (_colorMru != null) result += _colorMru.FullByteLength;
                if (_splitColors != null)
                result += _splitColors.FullByteLength;
                return result;
            }
        }

        public OfficeArtBStoreContainer BlipStore
        {
            get { return _blipStore; }
            set { _blipStore = value; }
        }

        public OfficeArtFDGGBlock DrawingGroup
        {
            get { return _drawingGroup; }
            set { _drawingGroup = value; }
        }

        public OfficeArtFOPT DrawingPrimaryOptions
        {
            get { return _drawingPrimaryOptions; }
            set { _drawingPrimaryOptions = value; }
        }

        protected override void ReadNoHeader(OfficeArtReadContext context)
        {
            Contract.Assert(rhrecVer == 0xF);
            Contract.Assert(rhrecInstance == 0x0);
            Contract.Assert(rhrecType == RecType.OfficeArtDggContainer);
            DrawingGroup = new OfficeArtFDGGBlock();
            DrawingGroup.Read(context);
            BlipStore = context.ReadOptionalRecord<OfficeArtBStoreContainer>(RecType.OfficeArtBStoreContainer);
            DrawingPrimaryOptions = context.ReadOptionalRecord<OfficeArtFOPT>(RecType.OfficeArtFOPT);
           // DrawingPrimaryOptions.Read(context);
            _drawingTertiaryOptions = context.ReadOptionalRecord<OfficeArtTertiaryFOPT>(RecType.OfficeArtTertiaryFOPT);
            _colorMru = context.ReadOptionalRecord<OfficeArtColorMRUContainer>(RecType.OfficeArtColorMRUContainer);
            _splitColors = context.ReadOptionalRecord<OfficeArtSplitMenuColorContainer>(RecType.OfficeArtSplitMenuColorContainer);
        }


        protected override void WriteNoHeader(OfficeArtWriteContext context)
        {
            Contract.Assert(rhrecVer == 0xF);
            Contract.Assert(rhrecInstance == 0x0);
            Contract.Assert(rhrecType == RecType.OfficeArtDggContainer);
            DrawingGroup.Write(context);
            if (BlipStore != null)
                BlipStore.Write(context);
            if (DrawingPrimaryOptions != null)
                DrawingPrimaryOptions.Write(context);
            if (_drawingTertiaryOptions != null)
                _drawingTertiaryOptions.Write(context);
            if (_colorMru != null)
                _colorMru.Write(context);
            if (_splitColors != null)
                _splitColors.Write(context);
        }


        public OfficeArtBStoreContainerFileBlock GetBlip(int blipId)
        {
            Contract.Assert(BlipStore != null);
            var item =  BlipStore.GetBlip(blipId);
            return item;
        }
    }
}